SpringBoot 笔记(十二):数据访问
1、JDBC
1 2 3 4 5 6 7 8 9
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
|
1 2 3 4 5 6
| spring: datasource: username: root password: 123456 url: jdbc:mysql://192.168.15.22:3306/jdbc driver-class-name: com.mysql.jdbc.Driver
|
1.默认情况下:
1. 用org.apache.tomcat.jdbc.pool.DataSource作为数据源;
2. 数据源的相关配置都在DataSourceProperties里面;
2.自动配置原理:
org.springframework.boot.autoconfigure.jdbc包下就是进行自动配置的包:
1、数据库连接池
参考 DataSourceConfiguration,根据配置创建数据源,默认使用Tomcat连接池,可以使用spring.datasource.type指定自定义的数据源类型
2、创建数据源
SpringBoot默认可以支持:org.apache.tomcat.jdbc.pool.DataSource、HikariDataSource、BasicDataSource
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@ConditionalOnMissingBean(DataSource.class) @ConditionalOnProperty(name = "spring.datasource.type") static class Generic {
@Bean public DataSource dataSource(DataSourceProperties properties) { return properties.initializeDataSourceBuilder().build(); }
}
|
3、DataSourceInitializer:ApplicationListener
一个监听器,主要用于sql脚本的运行
作用:
1)、runSchemaScripts();运行建表语句;
2)、runDataScripts();运行插入数据的sql语句;
默认只需要将文件命名为: schema-*.sql 或者 data-*.sql
默认规则:schema.sql,schema-all.sql
当然我们也可以在配置文件中指定,然后就加载指定的sql 脚本,这里是一个 list 也就是脚本可以指定多个。
1 2
| schema: - classpath:department.sql
|
2、整合Druid数据源
1.配置 yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| spring.datasource.initialSize:5 spring.datasource.minIdle:5 spring.datasource.maxActive:20 spring.datasource.maxlait:60800 spring.datasource.timeBetweenEvictionRunsMillis:60000 spring.datasource.minEvictableIdleTimeMillis:300000 spring.datasource.validationQuery:SELECT 1 FROM DUAL spring.datasource.testWhileIdle:true spring.datasource.testOnBorrow:false spring.datasource.testOnReturn:false spring.datasource.poolPreparedStatements:true
spring.datasource.filters:stat,wall,log4j spring.datasource.maxPoolPreparedStatementPerConnectionSize:20 spring.datasource.useGTobalDataSourceStat:true spring.datasource.connectionProperties:druid.stat.mergeSql=true;druid.stat.slowsqlMillis=5e0
|
上面的配置文件中的数据实际上我们没用上,这里我们需要创建一个 DruidConfig 然后和当前的配置文件绑定。
2.JavaConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| @Configuration public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin"); initParams.put("loginPassword","123456"); initParams.put("allow",""); initParams.put("deny","192.168.15.21");
bean.setInitParameters(initParams); return bean; }
@Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>(); initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean; } }
|
3、整合MyBatis
1 2 3 4 5
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency>
|
1、注解版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Mapper public interface DepartmentMapper {
@Select("select * from department where id=#{id}") public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}") public int deleteDeptById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id") @Insert("insert into department(departmentName) values(#{departmentName})") public int insertDept(Department department);
@Update("update department set departmentName=#{departmentName} where id=#{id}") public int updateDept(Department department); }
|
2.自定义配置
自定义MyBatis的配置规则,给容器中添加一个ConfigurationCustomizer;
1 2 3 4 5 6 7 8 9 10 11 12 13
| @org.springframework.context.annotation.Configuration public class MyBatisConfig {
@Bean public ConfigurationCustomizer configurationCustomizer(){ return new ConfigurationCustomizer(){ @Override public void customize(Configuration configuration) { configuration.setMapUnderscoreToCamelCase(true); } }; } }
|
使用MapperScan批量扫描所有的Mapper接口,这样的话我们就不用在Mapper接口上写 @Mapper 注解了。就属于批量的注解了这些注解。
1 2 3 4 5 6 7
| @MapperScan(value = "com.springboot.mapper") @SpringBootApplication public class SpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMybatisApplication.class, args); } }
|
3、配置文件版
1 2 3
| mybatis: config-location: classpath:mybatis/mybatis-config.xml 指定全局配置文件的位置 mapper-locations: classpath:mybatis/mapper/*.xml 指定sql映射文件的位置
|
更多使用参照 http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
4、整合SpringData JPA
1)、SpringData简介
2)、整合SpringData JPA
JPA:ORM(Object Relational Mapping);
1)、编写一个实体类(bean)和数据表进行映射,并且配置好映射关系;
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Entity @Table(name = "tbl_user") public class User {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id;
@Column(name = "last_name",length = 50) private String lastName; @Column private String email;
|
2)、编写一个Dao接口来操作实体类对应的数据表(Repository)
1 2 3 4
| public interface UserRepository extends JpaRepository<User,Integer> { }
|
3)、基本的配置JpaProperties
1 2 3 4 5 6 7
| spring: jpa: hibernate:
ddl-auto: update
show-sql: true
|